package com.luoy.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author LUOYUAN
 * @create 2018-12-02-18:24
 */
public class IOHDFS {

    // 文件的上传
    @Test
    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "luoy");

        // 2 获取输出流
        FSDataOutputStream fos = fs.create(new Path("/user/luoy/output/luoy.txt"));

        // 3 获取输入流
        FileInputStream fis = new FileInputStream(new File("C:\\Users\\luoyuan\\Desktop\\luoyuan.txt"));

        try {
            // 4 流对接
            IOUtils.copyBytes(fis, fos, configuration);
        } catch (Exception e) {

        }finally {
            // 5 关闭资源
            IOUtils.closeStream(fis);
            IOUtils.closeStream(fos);
        }
    }

    // 下载文件
    @SuppressWarnings("resource")
    @Test
    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "luoy");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/user/luoy/luoyuan.txt"));

        // 3 创建输出流
        FileOutputStream fos = new FileOutputStream(new File("e:/luoyuan.txt"));

        try {
            // 4 流的对接
            IOUtils.copyBytes(fis, fos, configuration);
        } catch (Exception e) {

        }finally {
            // 5 关闭资源
            IOUtils.closeStream(fis);
            IOUtils.closeStream(fos);
        }
    }
}
